/**
* @author  rockydai(dzjdai@gmail.com)
* @version 创建时间：2008-9-7 下午03:12:58
* 类说明
*/
package org.nielin.bestwaper.core;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author rockydai
 *
 */
public class WebEntityContext implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -4604264617572783487L;
    
    private Map<String, String> validatedParams = new HashMap<String,String>();
    private HttpServletRequest request;
    private HttpServletResponse response;
    private String id;
    
    public Map<String, String> getValidatedParams() {
        return validatedParams;
    }

    public void setValidatedParams(Map<String, String> validatedParams) {
        this.validatedParams = validatedParams;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }
    
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    /**
     * 取参数值
     * @param paramName
     * @param validatedParams
     * @param request
     * @return
     */
    private String getParameter( String paramName ) {
        Map<String,String> validatedParams = getValidatedParams();
        HttpServletRequest request = getRequest();
        
        String paramValue = validatedParams.get(paramName);
        if ( paramValue == null )
        {
            paramValue = request.getParameter(paramName);
            if ( paramValue == null )
            {
                MyLogger.getNormal().error( "can't get param [" + paramName + "]" );
            }
            else
            {
                MyLogger.getNormal().warn( "get not validated param [" + paramName + "] [" + paramValue + "]"  );
            }
        }
        
        return paramValue;
    }
    
    public String getStringParameter( String paramName, String defaultValue ) {
        String paramValue = getParameter( paramName );
        if ( paramValue == null )
        {
            return defaultValue;
        }
        else {
            return paramValue;
        }
    }
    
    public int getIntParameter( String paramName, int defaultValue ) {
        String paramValue = getParameter( paramName );
        if ( paramValue == null )
        {
            return defaultValue;
        }
        else {
            try {
                int iParamValue = Integer.valueOf(paramValue).intValue();
                return iParamValue;
            }
            catch( NumberFormatException e )
            {
                MyLogger.getNormal().error( paramValue + " convert to int fail ", e);
                return defaultValue;
            }
        }
    }
    
    @SuppressWarnings("unchecked")
	public <T extends Enum<?>> T getEnumParameter( String paramName, T defaultValue ) {
        String paramValue = getParameter( paramName );
        if ( paramValue == null )
        {
            return defaultValue;
        }
        else {
            try {
                T enumValue = (T)Enum.valueOf(defaultValue.getClass(), paramValue);
                return enumValue;
            }
            catch( NumberFormatException e )
            {
                MyLogger.getNormal().error( paramValue + " convert to enum " + defaultValue.getClass() + " fail ", e);
                return defaultValue;
            }
        }    	
    }
    
    /**
     * 取session server中保存的用户信息
     * @return
     */
    public SessionUserBean getUser() {
        return (SessionUserBean)request.getAttribute( "userBean" );
    }
    
    /**
     * 得到当前玩家对象
     * @return
     * @throws IDNotFoundException
     */
    public Player getPlayer() throws IDNotFoundException {
        Player player = ObjFactoryBuilder.getDefaultObjFactory().get( getPlayerID() );
        return player;
    }    
    
    /**
     * 取session server中保存的用户ID
     * @return
     */
    public String getUserID() {
        SessionUserBean user = (SessionUserBean)request.getAttribute( "userBean" );
        return User.getUserID( user.getUserID() );
    }

    /**
     * 得到当前玩家的PlayerID
     * @return
     */
    public String getPlayerID() {
        SessionUserBean user = getUser();
        return user.getPlayerID();        
    }
    
    /**
     * 将bean设置到request中
     * @param beanId
     * @param bean
     */
    public void setBean( String beanId, Bean bean ) {
        request.setAttribute( beanId, bean );
    }
    
    /**
     * 将一组bean设置到request中
     * @param beanListName
     * @param beanList
     */
    public void setBeanList( String beanListName, List<? extends Bean> beanList ) {
        request.setAttribute( beanListName, beanList );
    }
    
    /**
     * 设置msg页面的提示信息
     * @param context
     * @param msg
     */
    public void setMsg( String msg ) {
        request.setAttribute("msg", msg);
    }

    /**
     * 设置MSG页面的跳转地址参数
     * @param content 页面显示的链接名
     * @param id page id
     * @param params 跳转参数
     */
    public void setJumpUrl( String content, String id, Map<String,String> params) {
        request.setAttribute("jumpWords", content);     
        
        StringBuffer param = new StringBuffer();
        for ( String paramName : params.keySet() ) {
            param.append( "&amp;" + paramName + "=" + (String)params.get(paramName) );
        }
        
        request.setAttribute("jumpUrl", id + param);
    }

    /**
     * 将属性设定到request中
     * @param name
     * @param value
     */
    public void setAttribute( String name, String value ) {
    	MyLogger.getNormal().debug( "context set [" + name + "] value[" + value + "]");
        request.setAttribute( name, value );
    }
    
    /**
     * 将属性设定到request中
     * @param name
     * @param value
     */
    public void setAttribute( String name, java.lang.Object value ) {
        request.setAttribute( name, value );
    }
    
    /**
     * 取session id
     * @return
     */
    public String getSid() {
    	return WebUtil.getSid( this.getRequest() );
    }

    /**
     * 设置自动跳转的页面的timeout
     * @param timeout
     */
    public void setTimeout(int timeout) {
    	request.setAttribute("timeout", timeout);
    }
}
 